// haojk/pages/bindPhone/index.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      canSendCode:true,
      mobile:'',
      codeText:"请输入验证码",
      code:'',
      password: '',
      repassword: '',
      mobileInputCanUse:false
     // getCodeEvent:getCode

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var $that = this;
      var memberinfo = wx.getStorageSync("memberinfo");

      var globalset = wx.getStorageSync("globalset");

      if (memberinfo.mobile != '' && memberinfo.mobile != null && memberinfo.mobile != undefined){
          $that.setData({ mobile: memberinfo.mobile, mobileInputCanUse: true})
      }
  
      $that.setData({ logo: globalset.logo})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  changeColor:function(e){
     
      var $that= this;
      $that.setData({
          showRedBorder: e.currentTarget.dataset.number
      })
  },
  changeColor2: function (e) {
      var $that = this;
      if (e.currentTarget.dataset.number=='0'){
          $that.setData({
              mobile: e.detail.value
          })
      }
      if (e.currentTarget.dataset.number == '1') {
          $that.setData({
              code: e.detail.value
          })
      }
      if (e.currentTarget.dataset.number == '2') {
          $that.setData({
              password: e.detail.value
          })
      }
      if (e.currentTarget.dataset.number == '3') {
          $that.setData({
              repassword: e.detail.value
          })
      }

      $that.setData({
          showRedBorder: 4,

      })
 
  },
  getCode:function(){
      var $that = this;

      //定时器
        var phoneOri = $that.data.mobile;
     
      
        var phone = phoneOri.replace(/(^\s*)|(\s*$)/g, "");
       

      
        var clock = '';
        var nums = 10;
        var btn;
       
      
        if (phone==""||phone==undefined||phone==null){
            wx.showModal({
                title: '',
                content: '手机号码不能为空',
                showCancel: false,
                success: function (res) {
                    if (res.confirm) {
                      
                    } else if (res.cancel) {
                      
                    }
                }
            })  
            return false;   
        }
        else if (!(/^1[34578]\d{9}$/.test(phone))) {
           
            wx.showModal({
                title: '',
                content: '手机号码格式不正确',
                showCancel: false,
                success: function (res) {
                    if (res.confirm) {
                     
                    } else if (res.cancel) {
                      
                    }
                }
            })
            return false;
        }
      app.util.request({
          url: 'entry/wxapp/Sms',
          showLoading: false,
          data: {
              "m": 'nets_haojk',
              "mobile": $that.data.mobile
          },
          method: 'post',
          success: function (response) {
              
              wx.showModal({
                  content: response.data.data.message,
                  showCancel: false,
                  success: function (res) {
                  }
              })
              if (response.data.data.status=="200"){
                  $that.sendCode();
              }
          }
      })
  },


   sendCode:function(){ 
       var $that = this;
       var clock = '';
       var nums = 60;

        $that.setData({codeText:nums+"秒后可重新获取"})

        clock = setInterval(function () {
         
            nums--;
            if (nums > 0) {
                $that.setData({ codeText: nums + "秒后可重新获取" })
                $that.setData({ canSendCode: false })
            } else {
                clearInterval(clock); //清除js定时器
                $that.setData({ canSendCode: true })

                $that.setData({ codeText:"获取验证码" })
                nums = 60; //重置时间
        
            }
        }, 1000); //一秒执行一次
    },
   confirmButton:function() {
       var $that = this;
       var phoneOri = $that.data.mobile;
       var phone = phoneOri.replace(/(^\s*)|(\s*$)/g, "");
       if (phone == "") {
    
           wx.showModal({
               content: '手机号码不能为空',
               showCancel: false,
               success: function (res) {
               }
           })
           return false;
       }
       if (!(/^1[34578]\d{9}$/.test(phone))) {
    
           wx.showModal({
               content: '手机号码格式不正确',
               showCancel: false,
               success: function (res) {
               }
           })
           return false;
       }
       var codeOri = $that.data.code;
       var code = codeOri.replace(/(^\s*)|(\s*$)/g, "");
       if (code == "") {

           wx.showModal({
               content: '验证码不能为空',
               showCancel: false,
               success: function (res) {
               }
           })
           return false;
       }
       var passwordOri = $that.data.password;
       var password = passwordOri.replace(/(^\s*)|(\s*$)/g, "");
     
       if (password == "") {
           wx.showModal({
               content: '密码不能为空',
               showCancel: false,
               success: function (res) {
               }
           })
           return false;
       }
       if (password.length < 6) {

           wx.showModal({
               content: '密码不安全,密码长度不足6位',
               showCancel: false,
               success: function (res) {
               }
           })
           return false;
       }
       var repasswordOri = $that.data.repassword;
       var repassword = repasswordOri.replace(/(^\s*)|(\s*$)/g, "");
       if (repassword == "") {

           wx.showModal({
               content: '请在次输入密码',
               showCancel: false,
               success: function (res) {
               }
           })
           return false;
       }
       if (repassword != password) {

           wx.showModal({
               content: '2次密码输入不一致!',
               showCancel: false,
               success: function (res) {
               }
           })
           return false;
       }

       app.util.request({
           url: 'entry/wxapp/BindMobile',
           showLoading: true,
           data: {
               "m": 'nets_haojk',
               "mobile": $that.data.mobile,
               'code': $that.data.code,
               'password': $that.data.password,
               'isbind':false
           },
           method: 'post',
           success: function (response) {
             
               if (response.data.data.status=="500"){
                   app.util.request({
                       url: 'entry/wxapp/BindMobile',
                       showLoading: true,
                       data: {
                           "m": 'nets_haojk',
                           "mobile": $that.data.mobile,
                           'code': $that.data.code,
                           'password': $that.data.password,
                           'isbind': 1
                       },
                       method: 'post',
                       success: function (response) {
                         
                           wx.showModal({
                               content: response.data.data.message,
                               showCancel: false,
                               success: function (res) {
                                   if (res.confirm) {
                                       wx.switchTab({
                                           url: '../my/index',
                                           success: function (e) {
                                               var page = getCurrentPages().pop();
                                               if (page == undefined || page == null) return;
                                               page.onLoad();
                                           }
                                       })
                                   } 
                               }
                           })
                       }
                   })
               }else {
                   wx.showModal({
                       content: response.data.data.message,
                       showCancel: false,
                       success: function (res) {
                           if (res.confirm) {
                               wx.switchTab({
                                   url: '../my/index',
                                   success: function (e) {
                                       var page = getCurrentPages().pop();
                                       if (page == undefined || page == null) return;
                                       page.onLoad();
                                   }
                               })
                           } 
                           
                       }
                   })
               }
                  
               
           }
       })
   }


})